package com.example.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.article.service.ArticleConfigService;
import com.example.article.service.ArticleContentService;
import com.example.article.service.ArticleFreemarkerService;
import com.example.article.service.ArticleService;
import com.example.common.constants.ArticleConstants;
import com.example.common.util.R;
import com.example.common.util.ResultCodeRecord;
import com.example.model.article.dto.ArticleDto;
import com.example.model.article.dto.ArticleHomeDto;
import com.example.model.article.dto.ArticleInfoDto;
import com.example.model.article.pojo.Article;
import com.example.model.article.pojo.ArticleConfig;
import com.example.model.article.pojo.ArticleContent;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/")
@Tag(name = "文章信息")
public class ArticleHomeController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleConfigService articleConfigService;
    @Autowired
    private ArticleContentService articleContentService;

    @PostMapping(value = "/article/load")
    @Operation(summary = "加载文章信息")
    public R load(@RequestBody ArticleHomeDto dto) {
        // Map< String, Object > map = articleService.load(dto, ArticleConstants.LOAD_DEFAULT);
        Object object = articleService.loadArticleListV2(dto, ArticleConstants.TYPE_MORE, true);
        if (object != null) {
            return R.ok("加载文章信息成功").data("articleList", object);
        } else {
            return R.error("加载文章信息失败");
        }
    }

    @PostMapping(value = "/article/loadmore")
    @Operation(summary = "加载更多文章信息")
    public R loadMore(@RequestBody ArticleHomeDto dto) {
        List<Article> articleList = articleService.load(dto, ArticleConstants.LOAD_MORE);
        if (articleList != null) {
            return R.ok("加载更多文章信息成功").data("articleList", articleList);
        } else {
            return R.error("加载更多文章信息失败");
        }
    }

    @PostMapping(value = "/article/loadnew")
    @Operation(summary = "加载最新文章信息")
    public R loadNew(@RequestBody ArticleHomeDto articleHomeDto) {
        List<Article> articleList = articleService.load(articleHomeDto, ArticleConstants.LOAD_NEW);
        if (articleList != null) {
            return R.ok("加载最新文章信息成功").data("articleList", articleList);
        } else {
            return R.error("加载最新文章信息失败");
        }
    }

    @PostMapping(value = "/local_article_behavior")
    @Operation(summary = "加载文章行为")
    public R localArticleBehavior(@RequestBody ArticleInfoDto articleInfoDto) {
        Object object = articleService.loadArticleBehavior(articleInfoDto);
        if (object != null) {
            return R.ok("加载文章行为成功").data("", object);
        } else {
            return R.error("加载文章行为失败");
        }
    }

    @PostMapping(value = "/article/addArticle")
    @Operation(summary = "增加文章")
    public R addArticle(@RequestBody ArticleDto articleDto) {
        // 检查参数
        if (articleDto == null) {
            return R.error(ResultCodeRecord.PARAM_INVALID);
        }
        try {
            Thread.sleep(3000);
        } catch (Exception e) {

        }
        Article article = new Article();
        BeanUtils.copyProperties(articleDto, article);
        if (articleDto.getArticleId() == null) {
            articleService.save(article);
            ArticleConfig articleConfig = new ArticleConfig(article.getArticleId());
            articleConfigService.save(articleConfig);
            // 保存文章内容
            ArticleContent articleContent = new ArticleContent();
            articleContent.setArticleId(article.getArticleId());
            articleContent.setArticleContent(articleDto.getContent());
            articleContentService.save(articleContent);
            // 如果添加成功，返回成功信息
            if (article.getArticleId() != null) {
                return R.ok("增加文章成功").data("articleId", article.getArticleId());
            } else {
                return R.error("增加文章失败");
            }
        } else {
            // 修改文章
            articleService.updateById(article);
            // 修改文章内容
            LambdaQueryWrapper<ArticleContent> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ArticleContent::getArticleId, article.getArticleId());
            ArticleContent articleContent = articleContentService.getOne(wrapper);
            articleContent.setArticleContent(articleDto.getContent());
            articleContentService.updateById(articleContent);
            // 如果更新成功，返回成功信息
            if (articleContent != null) {
                return R.ok("更新文章成功").data("articleId", article.getArticleId());
            } else {
                return R.error("更新文章失败");
            }
        }
    }

    @PutMapping(value = "/updateArticle")
    @Operation(summary = "修改文章")
    public R updateArticle(@RequestBody ArticleDto articleDto) {
        Article article = new Article();
        articleService.updateById(article);
        ArticleContent articleContent = articleContentService.getOne(Wrappers.< ArticleContent >lambdaQuery().eq(ArticleContent::getArticleId, article.getArticleId()));
        articleContent.setArticleContent(articleDto.getContent());
        articleContentService.updateById(articleContent);
        // 如果更新成功，返回成功信息
        if (articleContent != null) {
            return R.ok("更新文章成功").data("articleId", article.getArticleId());
        } else {
            return R.error("更新文章失败");
        }
    }
}